package com.dnjn.happiness.biz.handler;

import com.dnjn.happiness.biz.domain.Training;
import com.dnjn.happiness.biz.domain.TrainingRecord;
import com.dnjn.happiness.biz.service.ITrainingRecordService;
import com.dnjn.happiness.biz.service.ITrainingService;
import com.dnjn.happiness.common.amqp.AmqpSender;
import com.dnjn.happiness.common.core.constant.HappyConstants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.SysJobVO;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.score.vo.HappyTraining;
import com.dnjn.happiness.common.core.score.vo.HappyVO;
import com.dnjn.happiness.job.api.RemoteJobService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component("traingHandler")
public class TraingHandler {
    private static final String READY = "READY";
    private static final String PROCESSING = "PROCESSING";
    private static final String FINISHED = "FINISHED";
    @Autowired
    private ITrainingService trainingService;
    @Autowired
    private RemoteJobService jobService;

    @Autowired
    private ITrainingRecordService trainingRecordService;

    

    /**
     * 培训
     *
     * @param traingId
     * @param jobId
     */
    public void theTraingToStart(String traingId, String jobId) {
        //          开始问卷
        trainingService.lambdaUpdate().eq(Training::getTrainingId, traingId).set(Training::getTrainingStatus, "PROCESSING").update();
        //仅仅运行一次
        SysJobVO newJob = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(Long.parseLong(jobId), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            newJob = result.getData();
        }
        newJob.setStatus("1");
        jobService.changeStatusApi(newJob, SecurityConstants.INNER);

    }

    public void theTraingToEnd(String traingId, String jobId) {
//          开始问卷
        trainingService.lambdaUpdate().eq(Training::getTrainingId, traingId).set(Training::getTrainingStatus, "FINISHED").update();
        // todo 结束培训发mq
        List<TrainingRecord> list = trainingRecordService.lambdaQuery()
                .eq(TrainingRecord::getTrainingId,Long.parseLong(traingId)).eq(TrainingRecord::getDeleted,0)
                .eq(TrainingRecord::getTrainingRecordStatus,'N').list();

        list.stream().forEach(item->{
            HappyVO happyVO = new HappyVO();
            happyVO.setRtype(HappyConstants.EMO_USER_RTYPE_SURVEY);
            happyVO.setUserId(item.getUserId());
            HappyTraining happyTraining = new HappyTraining();
            happyTraining.setNotJoin(1);
            happyVO.setTraining(happyTraining);
            AmqpSender.sendArgs(happyVO);

        });
        //仅仅运行一次
        SysJobVO newJob = new SysJobVO();
        R<SysJobVO> result = jobService.getInfoById(Long.parseLong(jobId), SecurityConstants.INNER);
        if (StringUtils.isNotNull(result) && StringUtils.isNotNull(result.getData())) {
            newJob = result.getData();
        }
        newJob.setStatus("1");
        jobService.changeStatusApi(newJob, SecurityConstants.INNER);

    }
}
